Imports CrystalDecisions.CrystalReports.Engine

Public Class frmBaoCaoTongHopNhapXuatTheoDonVi
    Private m_baocao As String

    Public Sub New(ByVal baocao As String)
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        m_baocao = baocao
        cbbNhapXuat.Items.Add("Nhập")

        If (m_baocao = "BaoCaoTongHopNhapXuat") Then
            lblTieuDeForm.Text = "BÁO CÁO TỔNG HỢP NHẬP, XUẤT THEO ĐƠN VỊ"
            cbbNhapXuat.Items.Add("Xuất")
        ElseIf (m_baocao = "BaoCaoChiTietHangNhap") Then
            lblTieuDeForm.Text = "BÁO CÁO CHI TIẾT HÀNG NHẬP"
        ElseIf (m_baocao = "BaoCaoTongHopHangNhap") Then
            lblTieuDeForm.Text = "BÁO CÁO TỔNG HỢP HÀNG NHẬP"
        End If
        'cac loai bao cao dung chung frm nay
        'BaoCaoTongHopNhapXuat
        'BaoCaoChiTietHangNhap ' chi bao cao Nhap
        'BaoCaoTongHopHangNhap
    End Sub

    'Dim kho As String = ""
    Dim loai_kho As String = ""
    Dim dvgn As String = ""

    Private Sub frmBaoCaoTongHopNhapXuatTheoDonVi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbbNhapXuat.Focus()
        cbbNhapXuat.SelectedIndex = 0
        dtpTuNgay.Text = "01/01/" + CStr(Date.Now.Year)
        dtpDenNgay.Text = Date.Now.Date.ToString
        rbtSLKH.Checked = True
    End Sub

    Public Function KiemTraNhap() As Boolean
        If (cbbNhapXuat.SelectedIndex = -1) Then
            ShowMessages("Bạn chưa chọn Loại Báo Cáo")
            cbbNhapXuat.Focus()
            Return False
        End If
        If (dvgn = "") Then
            ShowMessages("Bạn chưa chọn Đơn vị giao nhận")
            txtDonVi.Focus()
            Return False
        End If
        Return True
    End Function

#Region "txt Kho, LoaiKho, DVGN .Enter"

    Private Sub txtLoaiKho_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLoaiKho.DoubleClick
        Dim strDMLoaiKho As String
        strDMLoaiKho = "select KH,TEN from tblDMKhac where L = 'L'"
        Dim ma, ten As String
        ma = ""
        ten = ""
        If (txtLoaiKho.Text <> "") Then
            Dim strSql As String
            strSql = "select TEN from tblDMKhac where L = 'L' and KH = '" & txtLoaiKho.Text & "'"
            Dim ten_loaikho As String
            ten_loaikho = BaseDB.ExecSql_DataValue(strSql)
            If (Not ten_loaikho Is Nothing) Then
                loai_kho = txtLoaiKho.Text
                txtLoaiKho.Text = ten_loaikho
            Else
                'hien DM LoaiKho
                HienDM(strDMLoaiKho, "TEN", "KH", ten, ma)
                loai_kho = ma
                txtLoaiKho.Text = ten
            End If
        Else
            'hien DM LoaiKho
            HienDM(strDMLoaiKho, "TEN", "KH", ten, ma)
            loai_kho = ma
            txtLoaiKho.Text = ten
        End If
    End Sub

    Private Sub txtLoaiKho_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtLoaiKho.KeyDown
        If (e.KeyCode = Keys.Enter) Or (e.KeyCode = Keys.F1) Then
            txtLoaiKho_DoubleClick(txtLoaiKho, Nothing)
        End If
    End Sub

    Private Sub txtDonVi_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDonVi.DoubleClick
        ShowFrmChonDonVi()
    End Sub

    Private Sub txtDonVi_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDonVi.KeyDown
        If (e.KeyCode = Keys.Enter) Or (e.KeyCode = Keys.F1) Then
            ShowFrmChonDonVi()
        End If
    End Sub

    Public Sub ShowFrmChonDonVi()
        Dim frm As New frmDMDoiTuong
        frm.bCallForm = True
        frm.eDoiTuong = enumDOITUONG.DONVI
        frm.ShowDialog()
        If frm.selectedDT IsNot Nothing Then
            dvgn = frm.selectedDT.KH
            txtDonVi.Text = frm.selectedDT.Ten
        Else
            dvgn = ""
            txtDonVi.Text = ""
        End If
    End Sub

    Public Sub HienDM(ByVal strsql As String, ByVal strDM As String, ByVal strVM As String, _
                ByRef DM As String, ByRef VM As String)
        Dim frm As New frmChonDanhMucTheoListBox(strsql, strDM, strVM, "")
        frm.ShowDialog()
        DM = frm.DisplayMember
        VM = frm.ValueMember
    End Sub
#End Region

    Private Sub btnBaoCao_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBaoCao.Click
        If (KiemTraNhap()) Then
            Dim strSql As String
            Dim ds As DataSet
            '
            Dim tungay, denngay, tieude As String
            tungay = CStr(dtpTuNgay.Value.Month) + "/" + CStr(dtpTuNgay.Value.Day) + "/" + CStr(dtpTuNgay.Value.Year)
            denngay = CStr(dtpDenNgay.Value.Month) + "/" + CStr(dtpDenNgay.Value.Day) + "/" + CStr(dtpDenNgay.Value.Year)
            Dim sl_baocao As String
            If (rbtSLKH.Checked) Then
                sl_baocao = "SLKH"
            Else
                sl_baocao = "SLTH"
            End If
            '
            Dim nx As String
            If (cbbNhapXuat.SelectedIndex = 0) Then 'Nhap
                nx = "N"
                tieude = "NHẬP"
            Else 'Xuat
                nx = "X"
                tieude = "XUẤT"
            End If
            '
            Dim loaikho, thoigian, donvigiaonhan As String
            loaikho = "Kho :   " + txtLoaiKho.Text
            thoigian = "( Từ ngày   " + CStr(dtpTuNgay.Value.Day) + "/" + CStr(dtpTuNgay.Value.Month) + "/" + CStr(dtpTuNgay.Value.Year) + "    Đến ngày   " + CStr(dtpDenNgay.Value.Day) + "/" + CStr(dtpDenNgay.Value.Month) + "/" + CStr(dtpDenNgay.Value.Year) + " )"
            donvigiaonhan = "Đơn vị (Giao Nhận): " + dvgn + "    " + txtDonVi.Text

            If (m_baocao = "BaoCaoTongHopNhapXuat") Then
                strSql = "select * from viewTongHopKeHoachNhapXuat " & _
                "where DVGN = '" & dvgn & "' and lk = '" & loai_kho & "'"
                If (loai_kho <> "") Then
                    strSql &= " and lk = '" & loai_kho & "'"
                End If
                strSql &= " and NX = '" & nx & "'"
                strSql &= " and (NGAY >= '" + tungay + "' and NGAY <= '" + denngay + "')"
                strSql &= " order by ma"
                ds = BaseDB.ExecSql_DataSet(strSql)
                Dim nnreport As New rptTongHopNhapXuatTheoDonVi
                nnreport.SetDataSource(ds.Tables(0))
                nnreport.SetParameterValue(0, loaikho)
                nnreport.SetParameterValue(1, thoigian)
                nnreport.SetParameterValue(2, donvigiaonhan)
                nnreport.SetParameterValue(3, sl_baocao)
                tieude = "TỔNG HỢP KẾ HOẠCH " + tieude + " THEO ĐƠN VỊ"
                nnreport.SetParameterValue("tieude", tieude)
                Dim frm As New frmReportViewer(nnreport)
                frm.ShowDialog()
            ElseIf (m_baocao = "BaoCaoTongHopHangNhap") Then
                strSql = "select * from ViewTongHopHangNhap " & _
                "where DVGN = '" & dvgn & "'"
                If (loai_kho <> "") Then
                    strSql &= " and lk = '" & loai_kho & "'"
                End If
                strSql &= " and NX = '" & nx & "'"
                strSql &= " and (NGAY >= '" + tungay + "' and NGAY <= '" + denngay + "')"
                strSql &= " order by sohopdong,theolenh"
                ds = BaseDB.ExecSql_DataSet(strSql)
                Dim nnreport As New rptTongHopHangNhap
                nnreport.SetDataSource(ds.Tables(0))
                nnreport.SetParameterValue("sl_baocao", sl_baocao)
                nnreport.SetParameterValue("ngay", thoigian)
                nnreport.SetParameterValue("dvgn", donvigiaonhan)
                nnreport.SetParameterValue("loaikho", loaikho)
                nnreport.SetParameterValue("tieude", "BÁO CÁO TỔNG HỢP HÀNG " + tieude)

                Dim Section3 As Section = nnreport.ReportDefinition.Sections("Section3")
                Dim frmPrint As New frmPrintParamater("Chiều cao mỗi dòng mặc định", Section3.Height, "Giá trị mới", Section3.Height)
                frmPrint.ShowDialog()
                Section3.Height = frmPrint.value
                Dim frm As New frmReportViewer(nnreport)
                frm.ShowDialog()
            ElseIf (m_baocao = "BaoCaoChiTietHangNhap") Then
                strSql = "select * from ViewTongHopHangNhap " & _
                "where DVGN = '" & dvgn & "'"
                If (loai_kho <> "") Then
                    strSql &= " and lk = '" & loai_kho & "'"
                End If
                strSql &= " and NX = '" & nx & "'"
                strSql &= " and NX = 'N'"
                strSql &= " and (NGAY >= '" + tungay + "' and NGAY <= '" + denngay + "')"
                strSql &= " order by sohopdong,sophieu"
                ds = BaseDB.ExecSql_DataSet(strSql)
                Dim nnreport As New rptBaoCaoChiTietHangNhap
                nnreport.SetDataSource(ds.Tables(0))
                nnreport.SetParameterValue("sl_baocao", sl_baocao)
                nnreport.SetParameterValue("ngay", thoigian)
                nnreport.SetParameterValue("dvgn", donvigiaonhan)
                nnreport.SetParameterValue("loaikho", loaikho)
                nnreport.SetParameterValue("tieude", "BÁO CÁO CHI TIẾT HÀNG " + tieude)

                Dim Section3 As Section = nnreport.ReportDefinition.Sections("Section3")
                Dim DetailSection1 As Section = nnreport.ReportDefinition.Sections("DetailSection1")
                Dim frmPrint As New frmPrintParamater("Chiều cao mỗi dòng mặc định", Section3.Height, "Giá trị mới", Section3.Height)
                frmPrint.ShowDialog()
                Section3.Height = frmPrint.value
                DetailSection1.Height = frmPrint.value
                Dim frm As New frmReportViewer(nnreport)
                frm.ShowDialog()
            End If
        End If
    End Sub
End Class